<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use vquery;
use Redis;

/**
 * 默认控制器
 *
 * @author Sunxiaodong
 */
class Index extends Check {

    public function __construct() {
        parent::__construct();
        $this->left();
    }

    public function index() {

//        $redis = new Redis();
//        $redis->connect("127.0.0.1",6379);
//        $redis->set("name","123");
//        echo $redis->get("name");
        //$redis = new \Redis();
        //$redis->set("username", 'zhangsan', 3600);

        $title = "首页";
        session("cue", "首页");
        session("time", time());
        $uid = session("u_id"); //取出用户ID
        //获取公告
        $nlist = db("notice")->where("id=1")->find();
        //获取个人角色
        $jnum = db("zhanghao")->where("z_uid=$uid")->sum("z_num");
        //获取个人账号
        $znum = db("zhanghao")->where("z_uid=$uid")->count();
        $config = [
            'url' => $_SERVER['HTTP_HOST'],
            'document_root' => $_SERVER['DOCUMENT_ROOT'],
            'server_os' => PHP_OS,
            'server_port' => $_SERVER['SERVER_PORT'],
            'server_ip' => $_SERVER['SERVER_ADDR'],
            'server_soft' => $_SERVER['SERVER_SOFTWARE'],
            'php_version' => PHP_VERSION,
            'max_upload_size' => ini_get('upload_max_filesize')
        ];
        $this->assign('config', $config);
        $this->assign("jnum", $jnum);
        $this->assign("znum", $znum);
        $this->assign("title", $title);
        $this->assign("nlist", $nlist);
        return $this->fetch();
    }

    /**
     * 左侧菜单
     * @author Sunxiaodong
     */
    public function left() {
        //$this->assign("name", "2");
        $rid = session("u_rid"); //取出用户权限ID
        //根据用户ID找出权限ID
        if ($rid == 1) {
            //总管理员 查询所有

            $conelist = cache("onelist");

            $ctwolist = cache("twolist");

            if (empty($conelist) || empty($ctwolist)) {
                $onelist = db("auth")->where("auth_level=0")->select(); //一级菜单
                $twolist = db("auth")->where("auth_level=1")->select(); //二级菜单
                cache("onelist", $onelist, 0);
                cache("twolist", $ctwolist, 0);
            }

            $this->assign('onelist', $onelist);

            $this->assign('twolist', $twolist);
        } else {
            $conelist = cache("onelist");

            $ctwolist = cache("twolist");

            $u_menu = db("role")->where("r_id=$rid")->value("r_menu"); //用户对应权限

            if (empty($conelist) || empty($ctwolist)) {
                $onelist = db('auth')->where("auth_pid=0 and auth_id in ($u_menu)")->select(); //一级菜单
                $twolist = db('auth')->where("auth_pid!=0 and auth_id in ($u_menu)")->select(); //二级菜单
                cache("onelist", $onelist, 0);
                cache("twolist", $ctwolist, 0);
            }

            $this->assign('onelist', $onelist);
            $this->assign('twolist', $twolist);
        }

        return $this->fetch("public/left");
    }

    /**
     * 修改资料
     */
    public function pass() {

        $uid = session("u_id"); //取出用户ID
        $list = db("users")->where("u_id=$uid")->find();
        $title = "修改密码";
        session("cue", "修改密码");
        $this->assign("title", $title);
        $this->assign("list", $list);
        return $this->fetch();
    }

    //修改个人信息
    public function personal() {

        $file = request()->file("img");
        $uid = session("u_id"); //取出用户ID
        $data = input("post.");
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
            if ($info) {

                $data['img'] = $info->getSaveName();
            }

            $rule = [
                'img|头像' => 'require',
            ];
            $message = [
                'img.require' => '请设置头像！',
            ];
        }
        $rule = [
            'u_username|用户名' => 'require|max:20',
            'u_phone|手机号码' => 'require|length:11',
            'u_qq|QQ号码' => 'require|max:13|number',
        ];
        $message = [
            'u_username.require' => '请填写用户名！',
            'u_username.max' => '用户名长度不正确！',
            'u_phone.require' => '请填写手机号！',
            'u_phone.length' => '手机号码格式不正确！',
            'u_qq.require' => '请填写QQ号！',
            'u_qq.max' => 'QQ号长度不正确！',
            'u_qq.number' => 'QQ号只能为数字！',
        ];
//自定义规则

        $res = $this->validate($data, $rule, $message);

        if (true !== $res) {

            return ["status" => -1, 'message' => $res];
        }

        if (db('users')->where("u_id=$uid")->update($data)) {
            return ["status" => 1, 'message' => "保存成功！"];
        } else {
            return ["status" => 2, 'message' => "保存失败！"];
        }
    }

    //修改密码
    public function updatepass() {
        $uid = session("u_id"); //取出用户ID
        $upwd = input("u_password"); //原密码
        $npwd = input("n_password"); //新密码
        $data = input("post.");

        $oldpwd = db("users")->where("u_id=$uid")->value("u_password"); //比对原密码是否正确

        $rule = [
            'u_password|原密码' => 'require|max:15',
            'n_password|新密码' => 'require|max:15|alphaDash|min:6',
            'q_password|确认密码' => 'require|max:15|confirm:n_password',
        ];

        $message = [
            'u_password.require' => '请填写原密码！',
            'n_password.require' => '请填写新密码！',
            'q_password.require' => '请填写确认密码！',
            'u_password.max' => '原密码长度超出15个字符！',
            'n_password.min' => '密码长度6-15个字符！',
            'n_password.max' => '新密码长度超出15个字符！',
            'n_password.alphaDash' => '密码只允许字母数字下划线！',
            'q_password.max' => '确认密码长度超出15个字符！',
            'q_password.confirm' => '两次密码不一致',
        ];
        $res = $this->validate($data, $rule, $message);

        if (true !== $res) {

            return ["status" => -1, 'message' => $res];
        }
        if ($oldpwd != md5($upwd)) {
            return ["status" => 3, 'message' => "原密码输入不正确！"];
        }
        if ($oldpwd == md5($npwd)) {
            return ["status" => 3, 'message' => "原密码与新密码一致！"];
        }
        if (db("users")->where("u_id=$uid")->setField("u_password", md5($npwd))) {
            return ["status" => 1, 'message' => "密码修改成功,请重新登录！"];
        } else {
            return ["status" => 2, 'message' => "密码修改失败"];
        }
    }

//注销登录

    public function logout() {
        Session::clear();
        $this->redirect("/register");
    }

}
